package com.badibadi.fragment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

import com.badibadi.activity.UpPicturesActivity;
import com.badibadi.adapter.MyPolyAdapter;
import com.badibadi.fragment.MyPolyFragment.SetMyPolyAdapter;
import com.badibadi.infos.PhotoModel;
import com.badibadi.infos.Results;
import com.badibadi.interfaces.PolyInterface;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;

public class MyPolyFragment3 extends BaseFragment {
	static MyPolyFragment3 myPolyFragment3;
	private GridView mGridView;
	private MyPolyAdapter myPolyAdapter;
	private List<PhotoModel> mList;
	private String uid;
	private String fid;
	private PolyInterface polyInterface;
	/**
	 * 显示什么
	 */
	private int type = 2;// 0足迹，1梦想 2收藏 -1 全部
	// 版型 有三种 默认0 1参考 ， 2四个格子
	private String Version = "0";
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(getActivity());
				try {
					Utils.showMessage(getActivity(), getResources()
							.getString(R.string.l_net_error));
				} catch (Exception e) {
					// TODO: handle exception
					// throw new NullPointerException("getActivity() is Null");
				}

				break;
			case 2:
				Utils.ExitPrgress(getActivity());
				// initFragment();
				setMyAdapter();
				// xListView1.requestLayout();
				// x1Adapter.notifyDataSetChanged();
				break;
			case 3:
				Utils.ExitPrgress(getActivity());
				try {
					Utils.showMessage(getActivity(), getResources()
							.getString(R.string.l_xa10));
				} catch (Exception e) {
					// TODO: handle exception
					// throw new NullPointerException("getActivity() is Null");
				}
				break;
			case 4:
				Utils.ExitPrgress(getActivity());
				try {
					// Utils.showMessage(MyPoly1Activity.this,
					// results2.getRetmsg());
				} catch (Exception e) {
					// TODO: handle exception
					// throw new NullPointerException("getActivity() is Null");
				}
			default:
				break;
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		uid = getArguments().getString("uid", "26");
		fid = getArguments().getString("fid", "-1");
	}

	/**
	 * 设置适配器
	 * 
	 * @param view
	 */
	private void setMyAdapter() {
		myPolyAdapter = new MyPolyAdapter(getActivity(), mList, Version);
		mGridView.setAdapter(myPolyAdapter);
	}

	/**
	 * 初始化网络数据
	 */
	protected void initInternet() {
		// TODO Auto-generated method stub
		Utils.showPrgress(getActivity());
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", uid);
				if (type != -1) {
					map.put("type", type);
				}
				if (fid.equals(-1)) {
					map.put("fid", fid);
				}
				// if(polyInterface!=null)
				// {
				// polyInterface.setInterNetMap(map);
				// }

				String invite_content = Utils.sendRequest(map,
						Constants.BadiUrl + "photo");
				//    
				if (invite_content != null) {
					Results results = Utils.checkResult_NNN(getActivity(),
							invite_content);
					if (results != null && !results.getRetmsg().equals("null")) {
//						   
						try {
							mList = JSONUtils.getListByJsonString(
									results.getRetmsg(), PhotoModel.class);
							//    
							mHandler.sendEmptyMessage(2);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						mHandler.sendEmptyMessage(3);
					}
				} else {
					mHandler.sendEmptyMessage(1);
				}

			}
		});
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		View view = inflater.inflate(R.layout.fragment_my_poly_import_layout,
				null);
		mGridView = (GridView) view
				.findViewById(R.id.fragment_my_polygridView1);
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(),
						UpPicturesActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra("photoid", mList.get(arg2).getPid());
				intent.putExtra("photo_name", mList.get(arg2).getPhoto_name());
				intent.putExtra("type_upimg", "1");
				intent.putExtra("showType", "img");
				getActivity().startActivity(intent);
			}
		});
		initInternet();
		return view;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub

		super.onResume();
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	public static MyPolyFragment3 getInstance() {
		if (myPolyFragment3 == null) {
			return new MyPolyFragment3();
		}
		return myPolyFragment3;
	}

	// public void setInterNewClistener(PolyInterface interface1)
	// {
	// polyInterface=interface1;
	// }

	public void setPolyAdapter(SetMyPolyAdapter mypolyadapter) {
		mypolyadapter.setPolyAdapter(myPolyAdapter, mGridView, mList);
	}

	public interface SetMyPolyAdapter {
		void setPolyAdapter(MyPolyAdapter adapter, GridView mGridView,
				List<PhotoModel> list);
	}

}
